/**
 * This file contains javascript to handle information of chronic disease.
 */
//////////////////////// LOADING A PAGE ////////////
function loadPage(sourceDiv, actionURL, serviceId) {        
    $.ajax ({       
        url:        actionURL + "?set_ajax_view", 
        data:		"service_id=" + serviceId,	             
        dataType:   "html",
        beforeSend: function() {                    
            $('div#chronicDiseaseListContent').hide(); 
            $('div#pageContent').hide("slow");                
        },
        complete:   function() {
            $('div#' + sourceDiv).show("fast");         
        },
        success:  function(data) {
            $("div#" + sourceDiv).html(data);          
        }
    });
}

function changeChronicDiseaseStatus(chronicId, control) {
    $.ajax ({   
        url:        "ChangeChronicDiseaseStatusAjax?set_ajax_view",
        data:       "chronic_disease_id=" + chronicId,
        success:  function(data) {
            chronicTable = $('#chro_list');
            var html = '';
            var status = '';
            if (control == 'lock') {
                status = '<span class="impression">' + _("Delete") + '</span>';
                html = '<a class="btn" href="javascript:changeChronicDiseaseStatus(' + chronicId + ', \'unlock\');" title="'+ _("Unlock disease")+'">' 
                + '<img src="../images/unlock_small.png" alt="'+ _("Unlock") + '"></img></a>';
            } else if (control == 'unlock') {
                status = '<span>' + _("In Use") + '</span>';
                html = '<a class="btn" href="javascript:changeChronicDiseaseStatus(' + chronicId + ', \'lock\');" title="'+ _("Delete") + '">' 
                + '<img src="../images/lock_small.png" alt="'+ _("Delete") + '"></img></a>';
            }
            $('tr#'+ chronicId + ' td:nth-child(2)', chronicTable).html(status);
            $('tr#'+ chronicId + ' a.btn', chronicTable).parent().html(html);
        }
    });
}
function SaveChronicDisease() {
    // check chronic_disease_name
    if (!isNotEmpty($('#name'), _("Empty chronic disease name"), "status_message")) {
        return;
    }
    

    var submitData = $('#add_chronic_disease_form').formSerialize();
    $.ajax ({
            type:   "POST",
            url:    "SaveChronicDiseaseAjax?set_ajax_view",
            data:   submitData,
            beforeSend: function() {
                $('#btn_save').hide();
                $('#barLoading').show();                
            },
            complete:   function() {
                $('#btn_save').show();
                $('#barLoading').hide();
            },
            success:    function(data) {
                $('div#pageContent').hide('slow');
                loadPage('chronicDiseaseListContent', 'ListChronicDisease');
                $('div#chronicDiseaseListContent').show();
            }
        });
}
function EditChronicDisease(chronicId) {
    $('div#chronicDiseaseListContent').hide();
    // call ajax to load page
    loadPage('pageContent', 'EditChronicDisease', chronicId);
}